package com.zuimei.zuimei.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zuimei.zuimei.R;
import com.zuimei.zuimei.beans.FindAppItem5Bean;

import java.util.List;

/**
 * Created by Jogger on 2017/1/17.
 * 发现页recycleview条目适配器
 */

public class Item5RecycleViewItemAdapter extends RecyclerView.Adapter<Item5RecycleViewItemAdapter
        .MyViewHolder> {

    private List<Object> mList;
    private Context mContext;
    private static int mRank = 0;
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        void onItemClick(View view, FindAppItem5Bean.UserBean userBean);
    }

    public Item5RecycleViewItemAdapter(Context context) {
        mContext = context;
    }

    public void loadData(List<Object> list) {
        mList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (mList.get(position) instanceof String) {
            return 0;
            //标签布局
        } else if (mList.get(position) instanceof FindAppItem5Bean.UserBean) {
            //应用布局
            return 1;
        }
        return -1;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == 0) {
            view = LayoutInflater.from(mContext).inflate(R.layout.rv_item5_item_title, parent,
                    false);
        } else if (viewType == 1) {
            view = LayoutInflater.from(mContext).inflate(R.layout.rv_item5_item, parent,
                    false);
        }
        return new MyViewHolder(view);

    }


    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        int type = getItemViewType(position);
        Object o = mList.get(position);
        if (type == 0) {
            mRank = 0;
            holder.tv_label.setText("" + o);
        } else if (type == 1) {
            mRank++;
            final FindAppItem5Bean.UserBean userBean = (FindAppItem5Bean.UserBean) o;
            holder.tv_rank.setText(mRank + "");
            Picasso.with(mContext)
                    .load(userBean.getAvatar_url())
                    .config(Bitmap.Config.RGB_565)
                    .into(holder.iv_avatar);
            holder.tv_user_name.setText(userBean.getName());
            holder.tv_career.setText(userBean.getCareer());
            holder.tv_flowers.setText(userBean.getFlowers() + "");
            holder.rl_rank_container.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null) {
                        mListener.onItemClick(view, userBean);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_label;
        TextView tv_rank;
        TextView tv_user_name;
        ImageView iv_avatar;
        TextView tv_career;
        TextView tv_flowers;
        RelativeLayout rl_rank_container;

        MyViewHolder(View itemView) {
            super(itemView);
            tv_label = (TextView) itemView.findViewById(R.id.tv_label);
            tv_rank = (TextView) itemView.findViewById(R.id.tv_rank);
            tv_user_name = (TextView) itemView.findViewById(R.id.tv_user_name);
            iv_avatar = (ImageView) itemView.findViewById(R.id.iv_avatar);
            tv_career = (TextView) itemView.findViewById(R.id.tv_career);
            tv_flowers = (TextView) itemView.findViewById(R.id.tv_flowers);
            rl_rank_container = (RelativeLayout) itemView.findViewById(R.id.rl_rank_container);

        }
    }

}
